const app = getApp();
import { get } from '../../../utils/request';

Page({
  data: {
    loading: true,
    categories: [
      { id: 1, name: '日常保洁' },
      { id: 2, name: '深度清洁' },
      { id: 3, name: '家电清洗' },
      { id: 4, name: '上门维修' }
    ],
    activeTabIndex: 0,
    servicesData: [], // 所有服务数据
    currentServices: [] // 当前分类的服务
  },

  onLoad(options) {
    // 如果有传入分类ID，设置对应的激活标签
    if (options.categoryId) {
      const categoryIndex = this.data.categories.findIndex(item => item.id == options.categoryId);
      if (categoryIndex !== -1) {
        this.setData({
          activeTabIndex: categoryIndex
        });
      }
    }

    this.getServicesData();
  },

  // 模拟获取服务数据
  getServicesData() {
    this.setData({ loading: true });

    // 模拟请求，实际项目中应调用后端接口
    setTimeout(() => {
      // 模拟服务数据
      const servicesData = [
        {
          id: 1,
          categoryId: 1,
          title: '日常家庭保洁',
          desc: '专业阿姨上门服务，打造干净舒适的家居环境',
          price: 80,
          unit: '小时',
          coverImage: '/assets/images/services/daily-clean.jpg',
          saleCount: 253
        },
        {
          id: 2,
          categoryId: 1,
          title: '卧室整理',
          desc: '专业整理卧室空间，让您的休息环境更舒适',
          price: 60,
          unit: '小时',
          coverImage: '/assets/images/services/bedroom-clean.jpg',
          saleCount: 186
        },
        {
          id: 3,
          categoryId: 2,
          title: '厨房深度清洁',
          desc: '油烟机、灶台、橱柜等厨房设备的深度清洁',
          price: 280,
          unit: '次',
          coverImage: '/assets/images/services/kitchen-clean.jpg',
          saleCount: 186
        },
        {
          id: 4,
          categoryId: 2,
          title: '卫生间深度清洁',
          desc: '马桶、浴缸、淋浴房等卫浴设备的深度清洁',
          price: 220,
          unit: '次',
          coverImage: '/assets/images/services/bathroom-clean.jpg',
          saleCount: 168
        },
        {
          id: 5,
          categoryId: 3,
          title: '空调清洗',
          desc: '拆洗空调滤网和外壳，消毒除菌',
          price: 128,
          unit: '台',
          coverImage: '/assets/images/services/ac-clean.jpg',
          saleCount: 137
        },
        {
          id: 6,
          categoryId: 3,
          title: '冰箱清洗',
          desc: '深度清洁冰箱内外，除味除菌',
          price: 150,
          unit: '台',
          coverImage: '/assets/images/services/fridge-clean.jpg',
          saleCount: 98
        },
        {
          id: 7,
          categoryId: 4,
          title: '水管维修',
          desc: '解决漏水滴水问题，专业技师上门',
          price: 100,
          unit: '次',
          coverImage: '/assets/images/services/plumbing.jpg',
          saleCount: 75
        },
        {
          id: 8,
          categoryId: 4,
          title: '门锁安装维修',
          desc: '安装新门锁或修理损坏的门锁',
          price: 120,
          unit: '次',
          coverImage: '/assets/images/services/lock-repair.jpg',
          saleCount: 63
        }
      ];

      this.setData({
        servicesData,
        loading: false
      });

      // 初始化展示第一个分类的服务
      this.updateCurrentServices();
    }, 1000);
  },

  // 切换标签页
  onTabChange(e) {
    const index = e.detail.value;

    this.setData({
      activeTabIndex: index,
      loading: true
    });

    // 模拟加载数据
    setTimeout(() => {
      this.updateCurrentServices();
      this.setData({
        loading: false
      });
    }, 500);
  },

  // 更新当前分类的服务
  updateCurrentServices() {
    const { activeTabIndex, categories, servicesData } = this.data;
    const currentCategoryId = categories[activeTabIndex].id;

    // 筛选当前分类的服务
    const currentServices = servicesData.filter(item => item.categoryId === currentCategoryId);

    this.setData({
      currentServices
    });
  },

  // 跳转到服务详情页
  goToServiceDetail(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/service/detail/detail?id=${id}`
    });
  }
}); 